/*
* Copyright 2011 Stepan Riha
*
* This file may be used under the terms of of the
* GNU General Public License Version 3 or later (the "GPL"),
* http://www.gnu.org/licenses/gpl.html
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*/

var Prefs = (function() {

	var prefKey = function(name) {
		return "pref." + name;
	};

	var hasValue = function(name) {
		return localStorage[prefKey(name)] !== undefined;
	}
	
	var getValue = function(name, defaultValue) {
		var value = localStorage[prefKey(name)];
		if (value == undefined) {
			return defaultValue;
		}
		
		if(typeof(defaultValue) == "boolean") {
			return value === "true";
		}
		
		if(typeof(defaultValue) == "number") {
			return toInt(defaultValue) == defaultValue
				? parseInt(value)
				: parseFloat(value);
		}
		
		return value;
	};
	
	var setValue = function(name, value) {
		localStorage[prefKey(name)] = value;
	};

	var removeValue = function(name) {
		localStorage.removeItem(prefKey(name));
	};

	return {
		setValue: setValue,
		removeValue: removeValue,
		getValue: getValue
	};

})();

